Ext.ns("monitorPanelApply");
monitorPanelApply.init = function() {
	monitorPanelApply.big_Id;
	monitorPanelApply.lcm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				header : '姓名',
				dataIndex : 'name',
				sortable : true,
				width : 160
			}, {
				header : '性别',
				dataIndex : 'sex',
				sortable : true,
				width : 80
			}, {
				header : '专业',
				dataIndex : 'profession_Name',
				sortable : true,
				width : 80
			}]);
	monitorPanelApply.rcm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				header : '姓名',
				dataIndex : 'name',
				sortable : true,
				width : 160
			}, {
				header : '性别',
				dataIndex : 'sex',
				sortable : true,
				width : 80
			}, {
				header : '专业',
				dataIndex : 'profession_Name',
				sortable : true,
				width : 80
			}]);
	monitorPanelApply.lstore = new Ext.data.JsonStore({
				url : 'center/showMonitorListForMonitor',
				totalProperty : 'total',
				root : 'monitors',
				fields : [{
							name : "id",
							mapping : 'user_Id',
							type : 'int'
						}, {
							name : "name",
							mapping : 'user_RealName',
							type : 'string'
						}, {
							name : "sex",
							mapping : 'user_Sex',
							type : 'string'
						}, {
							name : "profession_Id",
							mapping : 'monitor_Big.big_Id',
							type : 'int'
						}, {
							name : "profession_Name",
							mapping : 'monitor_Big.big_Name',
							type : 'string'
						}]
			});
	monitorPanelApply.rstore = new Ext.data.JsonStore({
				url : 'center/autoArrangeMonitor',
				totalProperty : 'total',
				root : 'selectMonitor',
				fields : [{
							name : "id",
							mapping : 'user_Id',
							type : 'int'
						}, {
							name : "name",
							mapping : 'user_RealName',
							type : 'string'
						}, {
							name : "sex",
							mapping : 'user_Sex',
							type : 'string'
						}, {
							name : "profession_Id",
							mapping : 'monitor_Big.big_Id',
							type : 'int'
						}, {
							name : "profession_Name",
							mapping : 'monitor_Big.big_Name',
							type : 'string'
						}]
			});

	monitorPanelApply.leftPanel = new Ext.grid.GridPanel({
				cm : monitorPanelApply.lcm,
				store : monitorPanelApply.lstore,
				enableDragDrop : true,
				ddGroup : 'gridDDGroup2',
				scrollOffset : 0,
				stripeRows : true,
				viewConfig : {
					columnsText : '选择显示列',
					forceFit : true
				},
				loadMask : true,
				height : 500,
				frame : true,
				columnWidth : .5,
				tbar : new Ext.Toolbar(['-', {
							text : '重新安排',
							handler : function() {
								monitorPanelApply.rstore.removeAll();
							}
						}])
			});
	monitorPanelApply.rightPanel = new Ext.grid.GridPanel({
		cm : monitorPanelApply.rcm,
		store : monitorPanelApply.rstore,
		enableDragDrop : false,
		ddGroup : 'gridDDGroup2',
		stripeRows : true,
		columnWidth : .5,
		height : 500,
		frame : true,
		viewConfig : {
			forceFit : true
		},
		tbar : new Ext.Toolbar(['-', {
			text : '确定安排',
			handler : function() {
				if (monitorPanelApply.rstore.getCount() != 2) {
					Ext.Msg.show({
								title : '错误提示',
								msg : '对不起,添加的监考人员每个考场仅限2人！',
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.WARNING
							});
				} else {
					var monitor_Ids = []; // 要删除的id
					Ext.each(monitorPanelApply.rstore.getRange(),
							function(item) {
								monitor_Ids.push(item.data.id);
							});
					Ext.Ajax.request({
								url : 'center/confirmArrangeMonitor.action',
								method : 'post',
								params : {
									monitors_Id : monitor_Ids,
									position_Id : monitorApply.menuClick
											.get("id"),
									unit_Id : monitorApply.menuClick
											.get("unit_Id")
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									if (obj.success == false) {
										Ext.Msg.alert("提示", obj.msg);
									} else {
										monitorPanelApply.rstore.removeAll();
										Ext.Msg.alert("提示", obj.msg);
										monitorPanelApply.Main.hide();
										var index = monitorApply.store.find(
												"id", monitorApply.menuClick
														.get("id"));
										monitorApply.store.getAt(index).set(
												"state", "已分配");
									}
								},
								failure : function(response) {
									Ext.Msg.alert("错误","服务器连接失败！");
								}
							});
				}
			}
		}])
	});
	monitorPanelApply.rmenu = new Ext.menu.Menu({
				items : [{
					id : 'positionMenu',
					text : '删除该选项',
					handler : function() {
						monitorPanelApply.rstore
								.remove(monitorPanelApply.rmenuClick);
					}
				}]
			});
	monitorPanelApply.Main = new Ext.Window({
				region : "center",
				id : "monitorPanelApplyMainPanel",
				deferredRender : true,
				closeAction : 'hide',
				loadMask : true,
				width : 600,
				height : 400,
				closable : true,
				modal : true,
				progress : true,
				draggable : true,
				resizable : false,
				layout : 'column',
				autoScroll:true,
				viewConfig : {
					forceFit : true
				},
				items : [monitorPanelApply.leftPanel,
						monitorPanelApply.rightPanel]
			});

	monitorPanelApply.Main.on("show", function() {
		monitorPanelApply.Main.setTitle(monitorApply.menuClick.get("address")
				+ "——" + monitorApply.menuClick.get("name") + '考场' + '——'
				+ '监考人员安排窗口');
		new Ext.dd.DropTarget(
				monitorPanelApply.rightPanel.getView().scroller.dom, {
					ddGroup : 'gridDDGroup2',
					copy : false,
					notifyDrop : function(ddSource, e, data) {
						var records = ddSource.dragData.selections;
						Ext.each(records, function(item, index, allitems) {
									if (monitorPanelApply.rstore.find("id",
											item.data.id) == -1) {
										monitorPanelApply.rightPanel.store
												.add(item);
									} else {
										Ext.Msg.show({
													title : '错误提示',
													msg : '对不起，您已经添加了‘'
															+ item.data.name
															+ "'考场",
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.WARNING
												});
									}
								});
					}
				});
	});
	monitorPanelApply.rightPanel.on("rowcontextmenu", function(grid, rowIndex,
					e) {
				e.preventDefault();
				monitorPanelApply.rightPanel.getSelectionModel()
						.selectRow(rowIndex);
				monitorPanelApply.rmenu.showAt(e.getXY());
				monitorPanelApply.rmenuClick = monitorPanelApply.rightPanel
						.getSelectionModel().getSelected(rowIndex);
			});
};
Ext.onReady(monitorPanelApply.init);